"""
客户管理API路由
"""

from fastapi import APIRouter, HTTPException
from typing import List, Dict, Any

from models.customer_models import CustomerCreateRequest, CustomerUpdateRequest
from managers.stripe_manager import StripeManager

router = APIRouter(prefix="/api/customers", tags=["客户管理"])

# 创建全局的Stripe管理器实例
stripe_manager = StripeManager()


@router.post("/", response_model=Dict[str, Any])
async def create_customer(request: CustomerCreateRequest):
    """创建新客户"""
    return stripe_manager.customer_service.create_customer(request)


@router.get("/", response_model=List[Dict[str, Any]])
async def list_customers(limit: int = 100, offset: int = 0):
    """列出所有客户"""
    return stripe_manager.customer_service.list_customers(limit, offset)


@router.get("/{customer_id}", response_model=Dict[str, Any])
async def get_customer(customer_id: str):
    """获取客户信息"""
    customer = stripe_manager.customer_service.get_customer(customer_id)
    if not customer:
        raise HTTPException(status_code=404, detail="客户不存在")
    return customer


@router.put("/{customer_id}", response_model=Dict[str, Any])
async def update_customer(customer_id: str, request: CustomerUpdateRequest):
    """更新客户信息"""
    customer = stripe_manager.customer_service.update_customer(customer_id, request)
    if not customer:
        raise HTTPException(status_code=404, detail="客户不存在")
    return customer


@router.delete("/{customer_id}")
async def delete_customer(customer_id: str):
    """删除客户"""
    success = stripe_manager.customer_service.delete_customer(customer_id)
    if not success:
        raise HTTPException(status_code=404, detail="客户不存在")
    return {"message": "客户删除成功"}
